<?php
namespace app\back\model;

use think\Model;

class AccountRecord extends Model
{   
    const RECHARGE_TYPE = 2;
    const TIMES_RECHARGE_TYPE = 11;
    const TIMES_RECHARGE_DE_TYPE = 12;
    const GIVE_TYPE = 3;
    const CONSUMPTION_TYPE = 4;
    const SYSTEM_TYPE = 10;
    const SYSTEM_EDIT = 12;

    protected $table = 'account_record';

    function getRechargeTypeAttr($value) {
        $type = $this->rechargeTypeConfig();

        return isset($type[$value]) ? $type[$value] : '未知';
    }

    function rechargeTypeConfig() {
		return [AccountRecord::RECHARGE_TYPE=>'充值', AccountRecord::GIVE_TYPE=>'充值赠送',  '7'=>'系统赠送', '4'=>'消费', 
                '5'=>'朋友转赠', '6'=>'系统扣除',AccountRecord::SYSTEM_TYPE=>'新会员体验金',AccountRecord::TIMES_RECHARGE_TYPE=>'办卡充值',
                AccountRecord::TIMES_RECHARGE_DE_TYPE=>'办卡帐户扣除',AccountRecord::SYSTEM_EDIT=>'系统记录',
        ];
    }

    /*
	 * 统计个人消费情况
     */
    function customerStatistics($mobile) {
    	$sql = 'SELECT SUM(money) money, recharge_type FROM '.$this->table.' where mobile="'.$mobile.'" GROUP BY recharge_type';

    	$res = $this->db()->query($sql);
    	if(!empty($res)) {
    		foreach ($res as $key => $value) {
    			$res[$key]['recharge_type'] = $this->getRechargeTypeAttr($value['recharge_type']);
    		}
    	}
    	return $res;
    }
}
